/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FinisherLoanComputerRoom.java
 *
 * Created on 20/06/2011, 10:09:41 PM
 */

package GUI.ATTENDANT;

import GUI.Customer.Monitor;
import GUI.Customer.Server;
import Procedures.DBConection;
import Procedures.LoanTable;
import entities.Loan;
import java.util.Calendar;
import javax.swing.JFrame;

/**
 *
 * @author SIX-14
 */
public class FinisherLoanComputerRoom extends javax.swing.JPanel {

    /** Creates new form FinisherLoanComputerRoom */
    public FinisherLoanComputerRoom() {
        initComponents();
    }

    public FinisherLoanComputerRoom(DBConection myConection, JFrame paternFrame, LoanTable loantable, Server server) {
        initComponents();
        this.myConection=myConection;
        this.paternFrame=paternFrame;
        this.loanTable=loantable;
        this.server=server;
    }


    private DBConection myConection;
    private JFrame paternFrame;
    private LoanTable loanTable;
    private Server server;
    private Monitor monitor;
    private Loan loan;

    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }




    public LoanTable getLoanTable() {
        return loanTable;
    }

    public void setLoanTable(LoanTable loanTable) {
        this.loanTable = loanTable;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();

        jLabel1.setText("Identificador");

        jLabel2.setText("          ");

        jButton1.setText("Buscar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel3.setText("Salas Actualmente prestadas por este usuario:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addComponent(jButton1)))
                .addContainerGap(166, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addComponent(jButton1)
                .addContainerGap(122, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(!jTextField1.getText().isEmpty()){
            if(jButton1.getText().equals("Buscar")){
                if(getLoanTable().find_user(Integer.parseInt(jTextField1.getText()), "Sala")){
                    String[] computerRoom=null;
                    for(int i=0; i<getLoanTable().getTable().length; i++){
                        if(computerRoom==null){
                            computerRoom=new String[1];
                            computerRoom[0]=getLoanTable().getTable()[i].getComputerRoom();
                        }else{
                            for(int j=0; j<computerRoom.length;j++){
                                if(!computerRoom[j].equals(getLoanTable().getTable()[i].getComputerRoom())){
                                    String[] computerRoom2= new String[computerRoom.length+1];
                                    for(int k=0; k<computerRoom.length; k++){
                                        computerRoom2[k]=computerRoom[k];
                                    }
                                    computerRoom2[computerRoom2.length-1]=getLoanTable().getTable()[i].getComputerRoom();
                                    computerRoom=computerRoom2;
                                }
                            }
                        }
                    }
                    if (computerRoom != null) {
                        jComboBox1.removeAllItems();
                        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(computerRoom));
        }
                    
                    jLabel2.setText("tiene activo prestamo de sala");
                    jButton1.setText("Terminar prestamos de sala");
                }else{
                    System.out.println("no hay prestamo de salas de este usuario");
                }
            }else{
                String ip=null;
                while(getLoanTable().find_user(Integer.parseInt(jTextField1.getText()), "Sala")){
                    setLoan(getLoanTable().removeLoan(Integer.parseInt(jTextField1.getText()),"Sala",jComboBox1.getSelectedItem().toString()));
                setMonitor(getServer().getComstumer().receiveResult("/"+getLoan().getIpcomputer()));
                String endTime=(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)+":"+Calendar.getInstance().get(Calendar.MINUTE)+":"+Calendar.getInstance().get(Calendar.SECOND));
                getMyConection().sendSQL("INSERT INTO LOAN_COMPUTER SET DATE='"+getLoan().getDate()+"', ID_USER="+getLoan().getId()+", NUMBER_COMPUTER="+getLoan().getNumberComputer()+", STARTTIME='"+getLoan().getStartTime()+"', ENDTIME='"+endTime+"', DURATION="+getMonitor().getDuration()+";");
                for(int i=0; i<getMonitor().getProcess().length; i++){
                    if(getMonitor().getProcess()[i].getDuration()>0)
                getMyConection().sendSQL("INSERT INTO LOAN_COMPUTER_APLICATION SET COMPUTER="+getLoan().getNumberComputer()+", ID_USER="+getLoan().getId()+", DATE='"+getLoan().getDate()+"', DURATION="+getMonitor().getProcess()[i].getDuration()+", APLICATION='"+getMonitor().getProcess()[i].getName()+"';");
                }
                }
                getMyConection().sendSQL("UPDATE COMPUTER SET STATE=1 WHERE LETTERCOMPUTERROOM='" + jComboBox1.getSelectedItem().toString() + "';");
                this.setVisible(false);
                getPaternFrame().getContentPane().remove(this);
            }
        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
